package com.lc.communication.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.log.Log;
import com.lc.communication.vo.RequestVo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuhf17
 * @version 1.0.0
 * @ClassName HttpUtil.java
 * @Description 请求服务
 * @createTime 2024年06月12日 10:16:00
 */
public class LcHttpUtils {
    private static final Log log = Log.get();
    public static HttpResponse toRequest(RequestVo vo) {
        if ("GET".equalsIgnoreCase(vo.getMethod())) {
            HttpRequest request = HttpUtil.createGet(vo.getUrl());
            request.headerMap(vo.getHeader(), false);
            request.form(vo.getParams());
            return request.execute();
        } else if ("POST".equalsIgnoreCase(vo.getMethod())) {
            HttpRequest request = HttpUtil.createPost(vo.getUrl());
            request.headerMap(vo.getHeader(), false);
            request.form(vo.getParams());
            request.body(vo.getBody());
            return request.execute();
        } else if ("PATCH".equalsIgnoreCase(vo.getMethod())
                || "PUT".equalsIgnoreCase(vo.getMethod())
                || "DELETE".equalsIgnoreCase(vo.getMethod())) {
            HttpRequest request = HttpUtil.createRequest(Method.valueOf(vo.getMethod()), vo.getUrl());
            request.headerMap(vo.getHeader(), false);
            request.form(vo.getParams());
            request.body(vo.getBody());
            return request.execute();
        } else {
            log.info("开发中敬请期待。。。");
        }
        return null;
    }
}
